﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace RecruitmentProcessSystem
{
    public partial class frmInterviewManager : Form
    {
        String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection conn;
        SqlCommand cmd, cmd_add, cmd_edit, cmd_del;
        SqlDataAdapter da;
        DataSet ds, ds0, ds1, ds2;

        String iid = "";
        public frmInterviewManager()
        {
            InitializeComponent();
        }

        public void frmInterviewManager_Load(object sender, EventArgs e)
        {
            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
            ds = new DataSet();
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd = new SqlCommand("Select * from Interview", conn);
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "Interview");
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();


            ds0 = new DataSet();
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd = new SqlCommand("Select * from Nhanvien where not role = 'admin'", conn);
            da = new SqlDataAdapter(cmd);
            da.Fill(ds0, "Interviewer");
            cboInterview.DataSource = ds0.Tables[0];
            cboInterview.DisplayMember = "tennv";
            conn.Close();

            ds1 = new DataSet();
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd_add = new SqlCommand("Select aname from Applicant", conn);
            da = new SqlDataAdapter(cmd_add);
            da.Fill(ds1, "Applicant");
            cboApplicant.DataSource = ds1.Tables[0];
            cboApplicant.ValueMember = ds1.Tables[0].Columns[0].ToString();
            cboApplicant.DisplayMember = ds1.Tables[0].Columns[0].ToString();
            conn.Close();
            cboInterview.Text = "";
            cboVacancy.Text = "";
            cboApplicant.Text = "";
            txtStatus.Text = "";
            

        }

        private void btnCance_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

            try
            {
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
                DataGridViewRow row = new DataGridViewRow();
                row = dataGridView1.Rows[e.RowIndex];
                iid = row.Cells[0].Value.ToString();
                dtpDatetime.Text = row.Cells[1].Value.ToString();
                cboInterview.Text = row.Cells[2].Value.ToString();
                cboApplicant.Text = row.Cells[3].Value.ToString();
                cboVacancy.Text = row.Cells[4].Value.ToString();
                txtStatus.Text = row.Cells[5].Value.ToString();
            }
            catch { }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!iid.Equals(""))
            {
                if (MessageBox.Show("Do you want to delete?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    conn = new SqlConnection(conStr);
                    conn.Open();
                    cmd_del = new SqlCommand("Delete from Interview where iid = '" + iid + "'", conn);
                    cmd_del.ExecuteNonQuery();
                    conn.Close();
                    this.frmInterviewManager_Load(sender, e);
                    MessageBox.Show("Delete successfully!");
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnCreate_Click(object sender, EventArgs e)
        {

            conn = new SqlConnection(conStr);
            conn.Open();
            String time = dtpDatetime.Value.ToString();
            String interviewer = cboInterview.Text;
            String applicant = cboApplicant.Text;
            String vacancy = cboVacancy.Text;
            cmd_add = new SqlCommand("insert into Interview(idate,interviewer,aname,vname,istatus) values('" + time + "','" + interviewer + "','" + applicant + "','" + vacancy + "','" + txtStatus.Text + "')", conn);
            cmd_add.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("Creat new successfully!");
            this.frmInterviewManager_Load(sender, e);

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

            if (!iid.Equals(""))
            {
                conn = new SqlConnection(conStr);
                conn.Open();
                String time = dtpDatetime.Value.ToString();
                String interviewer = cboInterview.Text;
                String applicant = cboApplicant.Text;
                String vacancy = cboVacancy.Text;
                cmd_edit = new SqlCommand("update Interview set idate = '" + time + "',interviewer = '" + interviewer + "',aname = '" + applicant + "',vname = '" + vacancy + "',istatus = '" + txtStatus.Text + "' where iid = " + iid, conn);
                cmd_edit.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Update successfully!");
                this.frmInterviewManager_Load(sender, e);
                
            }
        }

        private void cboApplicant_SelectedValueChanged(object sender, EventArgs e)
        {
            ds2 = new DataSet();
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd_edit = new SqlCommand("Select vname from Applicant where aname='" + cboApplicant.SelectedValue.ToString() + "'", conn);
            da = new SqlDataAdapter(cmd_edit);
            da.Fill(ds2, "Vacancy");
            cboVacancy.DataSource = ds2.Tables[0];
            cboVacancy.ValueMember = ds2.Tables[0].Columns[0].ToString();
            cboVacancy.DisplayMember = ds2.Tables[0].Columns[0].ToString();
            conn.Close();
            
        }
    }
}